<template>
  <el-autocomplete
    v-model="localData"
    :fetch-suggestions="querySearch"
    :trigger-on-focus="true"
    clearable
    @select="handleSelect"
    @change="getValue"
    :key="index"
    maxlength="64"
    show-word-limit
  />
</template>
<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import {
    postMarketingContractMatchFirst,
    postMarketingContractMatchSecond,
    postMarketingContractMatchThird,
  } from '@/api/controller/marketing-contract-controller/index'
  import {
    postProcurementContractMatchFirst,
    postProcurementContractMatchSecond,
    postProcurementContractMatchThird,
  } from '@/api/controller/procurement-contract-controller/index'

  const props = defineProps({
    data: String,
    type: Number,
    listNum: Number,
  })

  const localData = ref(props.data)
  const restaurants = ref([])
  const index = ref(0)
  const contractType = ref(history.state?.contractType)
  onMounted(async () => {
    let res
    restaurants.value = []
    if (contractType.value === '1') {
      if (props.type === 1) {
        res = await postMarketingContractMatchFirst({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementName) {
              restaurants.value.push({ value: res.data[i].procurementName })
            }
          }
        }
        if (props.listNum === 2) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementNumber) {
              restaurants.value.push({ value: res.data[i].procurementNumber })
            }
          }
        }
        if (props.listNum === 3) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementAddress) {
              restaurants.value.push({ value: res.data[i].procurementAddress })
            }
          }
        }
      }
      if (props.type === 2) {
        res = await postMarketingContractMatchSecond({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorName) {
              restaurants.value.push({ value: res.data[i].vendorName })
            }
          }
        }
        if (props.listNum === 2) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorNumber) {
              restaurants.value.push({ value: res.data[i].vendorNumber })
            }
          }
        }
        if (props.listNum === 3) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorAddress) {
              restaurants.value.push({ value: res.data[i].vendorAddress })
            }
          }
        }
      }
      if (props.type === 3) {
        res = await postMarketingContractMatchThird({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].thirdName) {
              restaurants.value.push({ value: res.data[i].thirdName })
            }
          }
        }
      }
      if (props.listNum === 2) {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].thirdNumber) {
            restaurants.value.push({ value: res.data[i].thirdNumber })
          }
        }
      }
      if (props.listNum === 3) {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].thirdAddress) {
            restaurants.value.push({ value: res.data[i].thirdAddress })
          }
        }
      }
    } else {
      if (props.type === 1) {
        res = await postProcurementContractMatchFirst({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementName) {
              restaurants.value.push({ value: res.data[i].procurementName })
            }
          }
        }
        if (props.listNum === 2) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementNumber) {
              restaurants.value.push({ value: res.data[i].procurementNumber })
            }
          }
        }
        if (props.listNum === 3) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].procurementAddress) {
              restaurants.value.push({ value: res.data[i].procurementAddress })
            }
          }
        }
      }
      if (props.type === 2) {
        res = await postProcurementContractMatchSecond({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorName) {
              restaurants.value.push({ value: res.data[i].vendorName })
            }
          }
        }
        if (props.listNum === 2) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorNumber) {
              restaurants.value.push({ value: res.data[i].vendorNumber })
            }
          }
        }
        if (props.listNum === 3) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].vendorAddress) {
              restaurants.value.push({ value: res.data[i].vendorAddress })
            }
          }
        }
      }
      if (props.type === 3) {
        res = await postProcurementContractMatchThird({})
        if (props.listNum === 1) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].thirdName) {
              restaurants.value.push({ value: res.data[i].thirdName })
            }
          }
        }
        if (props.listNum === 2) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].thirdNumber) {
              restaurants.value.push({ value: res.data[i].thirdNumber })
            }
          }
        }
        if (props.listNum === 3) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].thirdAddress) {
              restaurants.value.push({ value: res.data[i].thirdAddress })
            }
          }
        }
      }
    }
    index.value++
  })

  const querySearch = (queryString: string, cb: any) => {
    let results = queryString
      ? restaurants.value.filter((item) => {
          return item.value.indexOf(queryString) >= 0
        })
      : restaurants.value
    if (queryString === 'null') {
      results = restaurants.value
    }
    cb(results)
  }

  const handleSelect = (item) => {
    localData.value = item.value
    getValue()
  }

  const getValue = () => {
    emit('getValue', localData.value)
  }
  const emit = defineEmits(['getValue'])
</script>
